package edu.neusoft.android.yuejian14.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.javabean.Train;
import edu.neusoft.android.yuejian14.model.TrainModel;

import static edu.neusoft.android.yuejian14.Const_Pic.BASE_URL;

public class TrainDetailActivity extends BaseActivity implements View.OnClickListener,RetrofitListener {
    private TextView train_name,train_intro,train_notice,train_time,train_address,train_money;
    private Button train_phone,train_add;
    private static ImageView trainpic;
    private ImageButton back;
    private int trainid;
    private int uid;


    @Override
    protected void initViews(){
        setLayout(R.layout.train_detail);
        train_name= (TextView) findViewById(R.id.train_name);
        train_intro= (TextView) findViewById(R.id.t_intro);
        train_notice= (TextView) findViewById(R.id.t_notice);
        train_time= (TextView) findViewById(R.id.t_time);
        train_address= (TextView) findViewById(R.id.t_address);
        trainpic=(ImageView) findViewById(R.id.pic);
        train_money= (TextView) findViewById(R.id.t_money);
        train_phone= (Button) findViewById(R.id.t_phone);
        train_add= (Button) findViewById(R.id.t_add);
        back=(ImageButton) findViewById(R.id.btn_back);
        train_add= (Button) findViewById(R.id.t_add);
        uid=getUid();

    }

    @Override
    View initViews(View view) {
        return view;
    }

    @Override
    void initEvents() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        train_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:13342230113"));
                startActivity(intent);
            }
        });

        train_add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                Intent intent =new Intent(TrainDetailActivity.this,ApplyActivity.class);
                Bundle bundle= getIntent().getExtras();
                trainid=bundle.getInt("trainid");
                uid=bundle.getInt("uid");
                intent.putExtra("trainid",trainid);
                intent.putExtra("uid",uid);
                startActivity(intent);
            }
        });
    }

    @Override
    void initData() {

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        trainid=bundle.getInt("trainid");

        TrainModel trainModel = new TrainModel();
        trainModel.getTrainById(String.valueOf(trainid),this);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        Train train= (Train) object;
        train_name.setText(train.getTrainname());
        train_intro.setText(train.getTrainintro());
        train_notice.setText(train.getTrainnotice());
        train_time.setText(train.getTraintime());
        train_address.setText(train.getTrainplace());
        train_money.setText("￥ "+ train.getTrainmoney()+"元/人");
        String trainpic = BASE_URL  + train.getTrainpic();
        if (!"".equals(train.getTrainpic()))
            Picasso.with(TrainDetailActivity.this).load(trainpic).error(R.drawable.no_pic).into(TrainDetailActivity.trainpic);
    }

    @Override
    public void onFail() {

    }

    @Override
    public void onClick(View view) {

    }

}